/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modules.legislation.gridcoverage;

import datatypes.RadiationPoint;

import java.util.ArrayList;

import modules.legislation.datatypes.AntennaGroup;
import modules.legislation.law.LawInterface;
import modules.pathloss.CoverageAdapter;
import tools.settings.SettingsMissingException;

/**
 * This class asks another class to calculate the SAR-values for each Sampled Point in the Sampled Grid.
 * The SAR-value is a combination of all the affecting antennas.
 * The SAR-values are matched against the Legislation-laws.
 *
 * The purpose of this class is to return only those sample points which exceed the
 * legislation laws.
 *
 * @author Pieter Backaert.
 */
public class CalculateSARValues {

    private RadiationPoint[][] radiationPointGrid;
    private AntennaGroup[][] antennaGroupGrid;
    private int SAMPLING_RESOLUTION;
    private LawInterface law;

    private ArrayList<RadiationPoint> radiationPointList;


    private CoverageAdapter ca;

    public CalculateSARValues(RadiationPoint[][] radiationPointGrid ,
                              AntennaGroup[][] antennaGroupGrid,
                              int SAMPLING_RESOLUTION ,
                              LawInterface law, CoverageAdapter ca) {
        this.radiationPointGrid = radiationPointGrid;
        this.antennaGroupGrid = antennaGroupGrid;
        this.SAMPLING_RESOLUTION = SAMPLING_RESOLUTION;
        this.law = law;
        this.ca = ca;

        radiationPointList = new ArrayList<RadiationPoint>();
    }

    public void setRadiationPointGrid(RadiationPoint[][] radiationPointGrid) {
        this.radiationPointGrid = radiationPointGrid;
    }

    public void setAntennaGroupGrid(AntennaGroup[][] antennaGroupGrid) {
        this.antennaGroupGrid = antennaGroupGrid;
    }



    //init !
    public void calculateALL() throws SettingsMissingException{
        // 4. For AntennaGangbang[][] : for each sampled grid-point :
        // calculate "the SAR" at this sampled point.
        //  -if SAR is too high : add to the RadiationPoint-list
        //  -if SAR is ok within limits, do nothing !
        radiationPointList.clear();
        for(int i=0 ; i<SAMPLING_RESOLUTION ; i++){
            for(int j=0 ; j<SAMPLING_RESOLUTION ; j++){
                if(antennaGroupGrid[i][j]!=null){
                    AntennaGroup agb = antennaGroupGrid[i][j];
                    if ( ! law.checkLegislation(agb.getAntennaAndDistance() ) ){ // if NIET-ok
                        RadiationPoint rp = radiationPointGrid[i][j]; //indexen kloppen per def.
                        rp.setSAR( law.getSAR_value() );
                        radiationPointGrid[i][j] = rp;
                        radiationPointList.add( rp );
                    }
                    else {
                        RadiationPoint rp = radiationPointGrid[i][j];
                        rp.setSAR(0);
                        radiationPointGrid[i][j]= rp;
                        //niks hier. want dit is de INIT-fase, onmogelijk om al dingen te verwijderen !
                    }
                }
            }//for
        }//for
    }//method


    //  bijna ok hieronder : maar nu ook nog rekening houden met de antenna's die
    // op INactive komen. idd, zit automatisch in  agb.getAntennaAndDistance() !!! DONE !!

    public void calculateForIndexBounds( int yMinIndex , int yMaxIndex , int xMinIndex , int xMaxIndex) throws SettingsMissingException{
        for(int i=yMinIndex ; i<=yMaxIndex ; i++){
            for(int j=xMinIndex ; j<=xMaxIndex ; j++){
                if(antennaGroupGrid[ antennaGroupGrid.length-1 -i][j]!=null){
                    AntennaGroup agb = antennaGroupGrid[antennaGroupGrid.length-1 -i][j];
                    if ( ! law.checkLegislation(agb.getAntennaAndDistance() ) ){ // if NIET-ok
                        RadiationPoint rp = radiationPointGrid[radiationPointGrid.length-1 -i][j]; //indexen kloppen per def.
                        radiationPointList.remove( rp) ; // verwijder de oude waarde uit de lijst

                        rp.setSAR( law.getSAR_value() );
                        radiationPointGrid[radiationPointGrid.length-1 -i][j] = rp;
                        radiationPointList.add( rp ); //"update" door de nieuwe waarde toe te voegen.
                    }
                    else {
                        RadiationPoint rp = radiationPointGrid[radiationPointGrid.length-1 -i][j]; //indexen kloppen per def.            
                        radiationPointList.remove( rp) ;

                        rp.setSAR(0);
                        radiationPointGrid[radiationPointGrid.length-1 -i][j]= rp;
                    }
                }

            }//for
        }//for
    }//method







    public RadiationPoint[] returnRadiationPointArray(){
        if(radiationPointList==null)
            return null;

        else if(radiationPointList.size()>0) {
            RadiationPoint[] array = new RadiationPoint[radiationPointList.size()];
            for(int i=0 ; i<array.length ; i++)
                array[i] = radiationPointList.get(i);

            return array;
        }
        else
            return null;
    }//method

}//class
